package edu.hit.crawler.io;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;

/**
 * Hadoop writable for a crawl item.
 * 
 * @author zczhang (zzc3615@gmail.com)
 * 
 */
public class CrawItem implements Writable {

	public static final float Initial_Score = 1.0f;
	/*
	 * public static final int SUCCESSED = 1; public static final int FAILED =
	 * 2; public static final int FORBIDDEN = 3; public static final int
	 * NOTFOUND = 4; public static final int RETRY = 5; public static final int
	 * MOVED = 5;
	 */
	public static final int SUCCESS = 1;
	public static final int FAILED = 2;
	public static final int UNCRAWL = 3;
	public static final int FORBIDDEN = 4;
	public static final int INJECTED = 5;
	

	public static final int GONE = 11;
	public static final int MOVED = 12;
	public static final int NOTFOUND = 14;
	public static final int RETRY = 15;
	public static final int EXCEPTION = 16;
	public static final int ACCESS_DENIED = 17;
	public static final int ROBOTS_DENIED = 18;
	public static final int REDIR_EXCEEDED = 19;
	public static final int NOTFETCHING = 20;
	public static final int NOTMODIFIED = 21;
	public static final int WOULDBLOCK = 22;
	public static final int BLOCKED = 23;

	private String url = new String();
	private int crawledStatus = UNCRAWL;
	// private float score = 1.0f;
	// private float newadd = 0f;
	private int outlinks = 0;

	private long lastModifyTime = 0;
	private long lastFetchTime = 0;
	private int refreshNum = 0;

	public CrawItem() {

	}

	public CrawItem(CrawItem it) {
		this.url = it.url;
		this.crawledStatus = it.crawledStatus;
		this.outlinks = it.outlinks;
		this.lastModifyTime = it.lastModifyTime;
		this.lastFetchTime = it.lastFetchTime;
		this.refreshNum = it.refreshNum;
	}

	public CrawItem(String url, int status) {
		this.url = url;
		this.crawledStatus = status;
	}

	public CrawItem(String url, int status, int outlinks) {
		this.url = url;
		this.crawledStatus = status;
		this.outlinks = outlinks;
	}

	public void setStatus(int crawledStatus) {
		this.crawledStatus = crawledStatus;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUrl() {
		return url;
	}

	public int getStatus() {
		return this.crawledStatus;
	}

	public int getOutlinks() {
		return outlinks;
	}

	public long getLastModified() {
		return lastModifyTime;
	}

	public void setLastModified(long lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}

	public long getLastFetchTime() {
		return lastFetchTime;
	}

	public void setLastFetchTime(long lastFetchTime) {
		this.lastFetchTime = lastFetchTime;
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		url = in.readUTF();
		crawledStatus = in.readInt();
		// score = in.readFloat();
		// newadd = in.readFloat();
		outlinks = in.readInt();
		lastModifyTime = in.readLong();
		lastFetchTime = in.readLong();
		refreshNum = in.readInt();
	}

	@Override
	public void write(DataOutput out) throws IOException {

		out.writeUTF(url);
		out.writeInt(crawledStatus);
		// out.writeFloat(score);
		// out.writeFloat(newadd);
		out.writeInt(outlinks);
		out.writeLong(lastModifyTime);
		out.writeLong(lastFetchTime);
		out.writeInt(refreshNum);
	}

	public String toString() {
		return "statu :		" + crawledStatus;

	}

	public int getRefreshNum() {
		return refreshNum;
	}

	public void setRefreshNum(int refreshNum) {
		this.refreshNum = refreshNum;
	}

	public void setOutlinks(int out) {
		outlinks = out;		
	}

}
